package com.agrm.brand.controller;


import com.agrm.brand.service.BrandService;
import com.agrm.model.brand.dto.BrandQuery;
import com.agrm.model.brand.pojo.Brand;
import com.agrm.model.common.dtos.PageRequestDto;
import com.agrm.model.common.dtos.PageResponseResult;
import com.agrm.model.common.dtos.ResponseResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("api/v1/brand")
public class BrandController {


    //TODO 品牌Redis缓存没加

    @Autowired
    private BrandService brandService;


    /**
     * 根据id查询品牌名称
     * @param id 品牌id
     * @return 品牌
     */
    @GetMapping("/get")
    public ResponseResult findById(@RequestParam("id") Integer id) {
        return ResponseResult.okResult(brandService.getById(id));
    }


    /**
     * 查询所有品牌
     * @return
     */
    @GetMapping("/getAll")
    public ResponseResult findAll() {
        return ResponseResult.okResult(brandService.list());
    }


    /**
     * 条件分页查询
     * @param page
     * @param query
     * @return
     */
    @GetMapping("/list")
    public PageResponseResult list(PageRequestDto page ,BrandQuery query){

        IPage<Brand> pages = brandService.queryList(page, query);
        PageResponseResult pageResponseResult = new PageResponseResult(page.getPage(), page.getSize(), (int) pages.getTotal());
        pageResponseResult.setData(pages.getRecords()); // 注意这里改为getRecords()

        return pageResponseResult;
    }

    /**
     * 修改品牌
     * @param brand
     * @return
     */
    @PutMapping("update")
    public ResponseResult update(@RequestBody Brand brand){
        brand.setUpdatedTime(LocalDateTime.now());
        return ResponseResult.okResult(brandService.updateById(brand));
    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("delete")
    public ResponseResult delete(@RequestBody List<Integer> ids){
        return ResponseResult.okResult(brandService.removeBatchByIds(ids));
    }


    /**
     * 添加品牌
     * @param brand
     * @return
     */
    @PostMapping("add")
    public ResponseResult add(@RequestBody Brand brand){
        brand.setCreatedTime(LocalDateTime.now());
        brand.setUpdatedTime(LocalDateTime.now());
        return ResponseResult.okResult(brandService.save(brand));
    }


    /**
     * 获取轮播图
     * @return
     */
    @PostMapping("resources/carousel")
    public ResponseResult getCarousel(){
        List<String> res = new ArrayList<>();
        res.add("https://java-2333.oss-cn-beijing.aliyuncs.com/2025/09/0018090575497102_b.jpg");
        res.add("https://java-2333.oss-cn-beijing.aliyuncs.com/2025/09/07099841_1.jpg");
        res.add("https://java-2333.oss-cn-beijing.aliyuncs.com/2025/09/16pic_4839143_b.jpg");
        return ResponseResult.okResult(res);
    }



}
